package com.kqb.auth.controller;

import com.aliyun.captcha20230305.Client;
import com.aliyun.captcha20230305.models.VerifyIntelligentCaptchaRequest;
import com.aliyun.captcha20230305.models.VerifyIntelligentCaptchaResponse;
import com.aliyun.tea.TeaException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 杉果树皮
 * @date 2025/4/17 10:27
 * @description:
 */

@RestController
@Api(tags = "阿里云验证码认证")
@RequestMapping("/auth")
@Slf4j
public class CaptchaController {

    @Autowired
    private Client captchaClient;

    @PostMapping("/verify")
    @ApiOperation("验证码校验")
    public Boolean verifyCaptcha(@RequestBody TemDTO temDTO) {

        String captchaVerifyParam = temDTO.getCaptchaVerifyParam();
        String sceneId = temDTO.getSceneId();
        VerifyIntelligentCaptchaRequest request = new VerifyIntelligentCaptchaRequest();
        request.sceneId = sceneId; // 来自前端
        request.captchaVerifyParam = captchaVerifyParam; // 来自前端

        try {
            VerifyIntelligentCaptchaResponse resp = captchaClient.verifyIntelligentCaptcha(request);
            // 建议使用您系统中的日志组件，打印返回
            // 获取验证码验证结果（请注意判空），将结果返回给前端。出现异常建议认为验证通过，优先保证业务可用，然后尽快排查异常原因。
            Boolean captchaVerifyResult = resp.body.result.verifyResult;
            String captchaVerifyCode = resp.body.result.verifyCode;
            return captchaVerifyResult;
            // 原因code

        } catch (Exception e) {

            // 建议使用您系统中的日志组件，打印异常
            // 出现异常建议认为验证通过，优先保证业务可用，然后尽快排查异常原因。


            return false;
        }


    }


}
